'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {

    // 创建新表，包含所有字段
    await queryInterface.createTable('sys_images', {
      id: {
        type: Sequelize.BIGINT,
        primaryKey: true,
        comment: '图片ID',
      },
      file_name: {
        type: Sequelize.STRING(255),
        allowNull: false,
        comment: '文件名',
      },
      file_path: {
        type: Sequelize.STRING(255),
        allowNull: false,
        comment: '文件路径',
      },
      file_size: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '文件大小(字节)',
      },
      file_type: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: '文件类型',
      },
      url: {
        type: Sequelize.STRING(255),
        allowNull: false,
        comment: '访问URL',
      },
      upload_time: {
        type: Sequelize.DATE,
        allowNull: false,
        comment: '上传时间',
      },
      status: {
        type: Sequelize.BOOLEAN,
        defaultValue: true,
        comment: '状态：true启用，false禁用',
      },
      create_time: {
        type: Sequelize.DATE,
        allowNull: false,
        comment: '创建时间',
      },
      update_time: {
        type: Sequelize.DATE,
        allowNull: false,
        comment: '更新时间',
      },
      category: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: '图片分类',
      },
      tags: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: '图片标签，JSON格式',
      },
      uploaded_by: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: '上传人用户名',
      },
      uploaded_by_id: {
        type: Sequelize.BIGINT,
        allowNull: true,
        comment: '上传人用户ID',
      },
    });

    // 添加索引
    await queryInterface.addIndex('sys_images', ['category'], {
      name: 'idx_images_category',
    });

    await queryInterface.addIndex('sys_images', ['uploaded_by_id'], {
      name: 'idx_images_uploaded_by_id',
    });

    await queryInterface.addIndex('sys_images', ['upload_time'], {
      name: 'idx_images_upload_time',
    });
  },

  down: async (queryInterface, Sequelize) => {
    // 先删除索引
    await queryInterface.removeIndex('sys_images', 'idx_images_category');
    await queryInterface.removeIndex('sys_images', 'idx_images_uploaded_by_id');
    await queryInterface.removeIndex('sys_images', 'idx_images_upload_time');
    // 再删除表
    await queryInterface.dropTable('sys_images');
  },
};